<template>
  <div class="visitor-container">
    <div class="top-item-group">
      <el-select v-model="serviceValue" clearable placeholder="全部客服组">
        <el-option
          v-for="item in serviceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-input
        style="width: 230px"
        placeholder="客服昵称/姓名/工号/手机号码"
        v-model="searchKeyword"
        clearable
      ></el-input>
      <el-button type="primary">搜索</el-button>

      <el-button style="float:right;">导出当前数据</el-button>
      <el-button @click="handlePartialDelete" style="float:right;">批量操作</el-button>
      <el-button style="float:right;" type="primary" @click.native.prevent="addDetail()">添加客服</el-button>
    </div>
    <div class="middle-table-group">
      <el-table
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="isLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="customerServiceId" label="ID" align="center">
          <template slot-scope="scope">
            <span style="color:#409EFF">{{scope.row.customerServiceId}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="账号" width="250" align="center"></el-table-column>
        <el-table-column prop="nickName" label="客服昵称" align="center"></el-table-column>
        <el-table-column prop="realName" label="客服姓名" align="center"></el-table-column>
        <el-table-column prop="serviceGroup" label="客户分组" align="center"></el-table-column>
        <el-table-column prop="serviceRole" label="客户角色" align="center"></el-table-column>
        <el-table-column prop="state" label="客户状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-value="在线"
              inactive-value="离线"
              @change="handleStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click.native.prevent="editDetail(scope.$index, tableData)" type="text" size="normal">编辑</el-button>
            <el-button @click="getDeleteForm(scope.row)" type="text" size="normal">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom-pagination-group">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :total="totalData"
        ></el-pagination>
      </div>
    </div>

    <el-dialog title="删除确认" :visible.sync="deleteDialogVisible" top="34vh" width="26%">
      <div class="delete-dialog-style">客服信息删除后无法恢复，确定删除此客服信息吗？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDelete">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  name: "SettingCsManagement",
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      serviceOptions: [
        {
          value: "选项1",
          label: "全部"
        },
        {
          value: "选项2",
          label: "客服组一"
        },
        {
          value: "选项3",
          label: "客服组二"
        },
        {
          value: "选项4",
          label: "客服组三"
        }
      ],
      serviceValue: "",
      sourceValue: "",
      searchKeyword: "",
      multipleSelection: [],
      deleteDialogVisible: false,
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      isLoading: false,
      deleteId: 0
    };
  },
  watch: {},
  computed: {},
  methods: {
    editDetail(index, tableData) {
      this.$router.push({
        name: "cs-management-detail",
        params: {
          type: "update",
          data: tableData[index]
        }
      });
    },
    addDetail() {
      this.$router.push({
        name: "cs-management-detail",
        params: {
          type: "create",
          data: {}
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    handleStateChange(row) {
      api
        .partialUpdateCustomerServiceAPI({
          customerServiceId: row.customerServiceId,
          state: row.state
        })
        .then(res => {
          let { error, data } = res.data;
          if (error) {
            this.$message.error(data);
          } else {
            this.$message({
              message: "修改客服状态成功",
              type: "success"
            });
          }
        });
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllCustomerServiceAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error(data);
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    },
    handlePartialDelete() {
      let params = this.multipleSelection.map(item => {
        return item.customerServiceId;
      });
      params = params.join(",");
      api.partialDeleteCustomerServiceAPI(params).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.multipleSelection = [];
          this.getPageData();
          this.$message({
            message: "客服信息删除成功",
            type: "success"
          });
        }
      });
    },
    getDeleteForm(data) {
      this.deleteDialogVisible = true;
      this.deleteId = data.customerServiceId;
    },
    handleDelete() {
      this.deleteDialogVisible = false;
      api.deleteCustomerServiceAPI(this.deleteId).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.getPageData();
          this.$message({
            message: "删除客服成功",
            type: "success"
          });
        }
      });
    }
  },
  created() {
    this.getPageData();
  },
  mounted() {}
};
</script>

<style scoped>
.top-item-group {
  padding-bottom: 20px;
}
.visitor-container {
  /* margin: 20px; */
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  float: right;
}
.el-select {
  width: 162px;
}
.el-input {
  width: 200px;
}
.delete-dialog-style {
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
  padding: 20px;
  padding-bottom: 70px;
}
.middle-table-group {
  width: 100%;
  display: inline-block;
}
</style>
